package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsTechnologyCommunicate;
import com.ruoyi.system.domain.news.dto.NewsTechnologyCommunicateDto;
import com.ruoyi.system.domain.news.query.NewsTechnologyCommunicateQuery;
import com.ruoyi.system.domain.news.vo.NewsTechnologyCommunicateVo;
import com.ruoyi.system.service.news.NewsTechnologyCommunicateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsTechnologyCommunicate")
@Api(value = "newsTechnologyCommunicateController", tags  = "学术研讨")
public class NewsTechnologyCommunicateController extends BaseController {

	@Resource
	private NewsTechnologyCommunicateService newsTechnologyCommunicateService;

    @ApiOperation(value = "新增/修改学术研讨信息", notes = "新增/修改学术研讨信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsTechnologyCommunicateDto newsTechnologyCommunicateDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsTechnologyCommunicate newsTechnologyCommunicate = new NewsTechnologyCommunicate();
		BeanUtils.copyProperties(newsTechnologyCommunicateDto,newsTechnologyCommunicate);
		if (newsTechnologyCommunicate.getId()==null) {
			newsTechnologyCommunicate.setCreateBy(loginUser.getUsername());
			newsTechnologyCommunicate.setDeptId(loginUser.getDeptId());
			newsTechnologyCommunicate.setCreateTime(new Date());
			newsTechnologyCommunicate.setUpdateBy(loginUser.getUsername());
			newsTechnologyCommunicate.setUpdateTime(new Date());
		} else {
			newsTechnologyCommunicate.setUpdateBy(loginUser.getUsername());
			newsTechnologyCommunicate.setUpdateTime(new Date());
		}
		boolean result = newsTechnologyCommunicateService.saveOrUpdate(newsTechnologyCommunicate);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除学术研讨信息", notes = "删除学术研讨信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsTechnologyCommunicateService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个学术研讨信息", notes = "获取单个学术研讨信息")
	@GetMapping("/getById")
	public R<NewsTechnologyCommunicateVo> getById(@RequestParam(value = "id") Long id) {
		NewsTechnologyCommunicateQuery newsTechnologyCommunicateQuery = new NewsTechnologyCommunicateQuery();
		newsTechnologyCommunicateQuery.setId(id);
		List<NewsTechnologyCommunicateVo> newsTechnologyCommunicateList = newsTechnologyCommunicateService.getNewsTechnologyCommunicateList(newsTechnologyCommunicateQuery);
		if (newsTechnologyCommunicateList!=null&&newsTechnologyCommunicateList.size()>0) {
			return R.ok(newsTechnologyCommunicateList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取学术研讨信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsTechnologyCommunicateVo>> list(@RequestBody NewsTechnologyCommunicateQuery newsTechnologyCommunicateQuery) {
		List<NewsTechnologyCommunicateVo> newsTechnologyCommunicateList = newsTechnologyCommunicateService.getNewsTechnologyCommunicateList(newsTechnologyCommunicateQuery);
		return R.ok(new PageInfo<>(newsTechnologyCommunicateList));
	}
}
